package com.sf.sgs.sessionevaluate.service.impl;

import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sf.sgs.sessionevaluate.controller.dto.req.QuestionAnswerReq;
import com.sf.sgs.sessionevaluate.dao.IQuestionAnswerao;
import com.sf.sgs.sessionevaluate.domain.QuestionAnswer;
import com.sf.sgs.sessionevaluate.domain.QuestionOption;
import com.sf.sgs.sessionevaluate.service.IQuestionAnswerService;
import com.sf.sgs.sessionevaluate.service.IQuestionOptionService;
import com.sf.sgs.sessionevaluate.util.BeanUtil;

@Component
public class QuestionAnswerServiceImpl extends TransactionalService implements IQuestionAnswerService {

	@Autowired
	private IQuestionAnswerao questionAnswerao;

	@Autowired
	private IQuestionOptionService questionOptionService;

	@Override
	public void saveAnswerForOption(List<QuestionAnswerReq> questionAnswerReqList) {
		questionAnswerReqList.stream().forEach(new Consumer<QuestionAnswerReq>() {

			@Override
			public void accept(QuestionAnswerReq questionAnswerReq) {
				QuestionAnswer questionAnswer = BeanUtil.copyProperties(questionAnswerReq, QuestionAnswer.class, "optionId");
				QuestionOption questionOption = questionOptionService.getById(questionAnswerReq.getOptionId());
				questionAnswer.setQuestionOption(questionOption);
				questionAnswer.setQuestion(questionOption.getQuestion());
				questionAnswer.setQuestionnaire(questionOption.getQuestion().getQuestionnaire());
				questionAnswer.setCreateDate(new Date());
				questionAnswerao.save(questionAnswer);
			}
		});

	}

}
